package com.ymatou.iapi.coupon.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.coupon.parameter.sendgiftcouponBean;
import com.ymatou.iapi.coupon.service.sendgiftcouponCall;
import com.ymatou.iapi.optrade.parameter.enums.CancelOrderTypeEnum;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceHunterOrderCallService;
import com.ymttest.database.model.coupon;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sqlwapper.couponWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*用户购买全球优选大礼包发券
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_sendgiftcoupon{
	private static sendgiftcouponBean sendgiftcouponbean;
	private static sendgiftcouponCall sendgiftcouponcall;
	private static long userId = Long.valueOf(EnvSetup.getData("BuyUserId"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("用户购买全球优选大礼包发券");
	}

	@Before
	public void caseUp() {
		sendgiftcouponbean=new sendgiftcouponBean();
		sendgiftcouponcall=new sendgiftcouponCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sendgiftcouponcall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_sendgiftcoupon_001() {
		Logger.start(true,"用户购买全球优选大礼包发券");
		try {
			Logger.comment("######删除未支付订单 开始#######");
			OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
			List<OrderPo> orderList = orderShardingWapper
					.selectHunterGiftBagOrderbyUserId(userId);
			if (orderList.size() > 0) {
				for (OrderPo po : orderList) {
					TradingCallService.cancelOrder(po.getOrderId(),
							CancelOrderTypeEnum.BUYER_CANCEL_ORDER, userId);
				}
			}
			Logger.comment("######删除未支付订单 结束#######");
			
			int hunterId = TestDataManager.getRandomIntNum(6);
			TradingServiceResp placeOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, 1);	
			TradingCallService.payOrder(userId,placeOrder.getPayableAmount(),placeOrder.getTradingId());
			
			sendgiftcouponbean.setHunterId((long)hunterId);
			sendgiftcouponbean.setHunterOrderType(1);
			sendgiftcouponbean.setOrderId(placeOrder.getMainOrderId());
			sendgiftcouponbean.setBuyerId((int)userId);
			
			sendgiftcouponcall.setData(sendgiftcouponbean);
			sendgiftcouponcall.callService();	
			Logger.verifyEquals("ok", sendgiftcouponcall.getReturnData(), "验证返回值");
			coupon coupon2=new couponWapper().selectByBatchIdAndUserId(0, sendgiftcouponbean.getBuyerId()).get(0);
			Logger.verifyEquals(1, coupon2.getPlatformSource(), "验证coupon表中的字段PlatformSource=1 平台来源是全球优选");
			Logger.verifyEquals(false, coupon2.getIsInvalid(), "验证coupon表中的字段IsInvalid=0有效优惠券");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_sendgiftcoupon_002() {
		Logger.start(true,"用户购买全球优选大礼包发券-订单为null");
		try {
			Logger.comment("######删除未支付订单 开始#######");
			OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
			List<OrderPo> orderList = orderShardingWapper
					.selectHunterGiftBagOrderbyUserId(userId);
			if (orderList.size() > 0) {
				for (OrderPo po : orderList) {
					TradingCallService.cancelOrder(po.getOrderId(),
							CancelOrderTypeEnum.BUYER_CANCEL_ORDER, userId);
				}
			}
			Logger.comment("######删除未支付订单 结束#######");
			
			int hunterId = TestDataManager.getRandomIntNum(6);
			TradingServiceResp placeOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, 1);	
			TradingCallService.payOrder(userId,placeOrder.getPayableAmount(),placeOrder.getTradingId());
			
			sendgiftcouponbean.setHunterId((long)hunterId);
			sendgiftcouponbean.setHunterOrderType(1);
			sendgiftcouponbean.setOrderId(null);
			sendgiftcouponbean.setBuyerId((int)userId);
			
			sendgiftcouponcall.setData(sendgiftcouponbean);
			sendgiftcouponcall.callService();	

			Logger.verifyEquals("failed", sendgiftcouponcall.getReturnData(), "验证返回值");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_sendgiftcoupon_003() {
		Logger.start(true,"用户购买全球优选大礼包发券-订单为0");
		try {
			Logger.comment("######删除未支付订单 开始#######");
			OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
			List<OrderPo> orderList = orderShardingWapper
					.selectHunterGiftBagOrderbyUserId(userId);
			if (orderList.size() > 0) {
				for (OrderPo po : orderList) {
					TradingCallService.cancelOrder(po.getOrderId(),
							CancelOrderTypeEnum.BUYER_CANCEL_ORDER, userId);
				}
			}
			Logger.comment("######删除未支付订单 结束#######");
			
			int hunterId = TestDataManager.getRandomIntNum(6);
			TradingServiceResp placeOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, 1);	
			TradingCallService.payOrder(userId,placeOrder.getPayableAmount(),placeOrder.getTradingId());
			
			sendgiftcouponbean.setHunterId((long)hunterId);
			sendgiftcouponbean.setHunterOrderType(1);
			sendgiftcouponbean.setOrderId((long)0);
			sendgiftcouponbean.setBuyerId((int)userId);
			
			sendgiftcouponcall.setData(sendgiftcouponbean);
			sendgiftcouponcall.callService();	

			Logger.verifyEquals("failed", sendgiftcouponcall.getReturnData(), "验证返回值");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}